<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<c:set var="deptId" value="${param.deptId}" />
<c:set var="deptParentId" value="${param.deptParentId}" />
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <title>信息总部管理</title>
    <link rel="stylesheet" href="${ctx}/css/xtable.css"/>
    <script type="text/javascript" src="${ctx}/js/validator.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/AreaService.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/DeptService.js"></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body style="text-align: center;">
<div style="width: 700px; margin:0 auto">
    <form id="deptForm" name="deptForm" method="post">
        <input type="hidden" id="deptId" name="deptId" value="${deptId}">
        <input type="hidden" id="deptParentId" name="deptParentId" value="${deptParentId}">
        <c:if test="${!empty deptParentId}">
            <input type="hidden" id="deptType" name="deptType" value="0">
        </c:if>
        <c:if test="${empty deptParentId}">
            <input type="hidden" id="deptType" name="deptType" value="1">
        </c:if>
        <table cellpadding="15" cellspacing="0" width="100%" border="1">
            <tr>
                <td>
                    信息部Logo
                </td>
                <td>
                    <input type="file" id="deptLogoFile" name="deptLogoFile" style="width: 300px">
                </td>
            </tr>
            <c:if test="${!empty deptParentId}">
                <tr>
                    <td width="200">
                        总部名称
                    </td>
                    <td id="deptParentName">
                    </td>
                </tr>
            </c:if>
            <tr>
                <td width="200">
                    信息部名称
                </td>
                <td>
                    <input type="text" id="deptName" name="deptName" maxlength="32" style="width: 300px" required>
                </td>
            </tr>
            <tr>
                <td>
                    信息部地区
                </td>
                <td>
                    <select id="provinceId" name="provinceId" placeholder="省" required="true" onchange="selectCityList()">
                        <option value="">选择省</option>
                    </select>
                    <select id="cityId" name="cityId" placeholder="市" required="true" onchange="selectDistrictList()">
                        <option value="">选择市</option>
                    </select>
                    <select id="districtId" name="districtId" placeholder="区" required="true">
                        <option value="">选择区</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    信息部地址
                </td>
                <td>
                    <input type="text" id="deptAddress" name="deptAddress" maxlength="32" style="width: 300px" required>
                </td>
            </tr>
            <tr>
                <td>
                    打印机SN
                </td>
                <td>
                    <input type="text" id="deptPrintSn" name="deptPrintSn" maxlength="32" style="width: 300px" required>
                </td>
            </tr>
            <tr>
                <td>
                    打印机KEY
                </td>
                <td>
                    <input type="text" id="deptPrintKey" name="deptPrintKey" maxlength="32" style="width: 300px" required>
                </td>
            </tr>
            <tr>
                <td>
                    试用期限
                </td>
                <td>
                    <input type="radio" id="deptTryDay3650" name="deptTryDay" value="3650" checked>
                    <label for="deptTryDay3650">永久</label>
                    <input type="radio" id="deptTryDay1" name="deptTryDay" value="30">
                    <label for="deptTryDay1">1个月</label>
                    <input type="radio" id="deptTryDay3" name="deptTryDay" value="90">
                    <label for="deptTryDay3">3个月</label>
                    <input type="radio" id="deptTryDay6" name="deptTryDay" value="180">
                    <label for="deptTryDay6">6个月</label>
                    <input type="radio" id="deptTryDay12" name="deptTryDay" value="365">
                    <label for="deptTryDay12">1年</label>
                </td>
            </tr>
            <tr>
                <td>
                    创建时间
                </td>
                <td id="deptCreateTime">
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <img src="${ctx}/images/common/button_save.jpg" title="保存" style="cursor:pointer" onclick="submitDept()"/>
                    <img src="${ctx}/images/common/button_back.jpg" title="返回" style="cursor:pointer" onclick="callback()"/>
                </td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
<script type="text/javascript">
    <c:if test="${!empty deptParentId}">
    var parentDept = new Dept();
    parentDept.deptId = "${deptParentId}";
    DeptService.selectDeptList(parentDept, callbackSelectParentDeptList);
    function callbackSelectParentDeptList(list) {
        parentDept = list[0];
        dwr.util.setValue("deptParentName", parentDept.deptName);
    }
    </c:if>
    dwr.util.setValue("deptCreateTime", new Date().Format("yyyy-MM-dd"));
    selectProvinceList();

    function selectProvinceList() {
        var province = new Province();
        AreaService.selectProvinceList(province, callbackSelectProvinceList);
    }
    function callbackSelectProvinceList(list) {
        dwr.util.addOptions("provinceId", list, "provinceId", "provinceName");
        if("${deptId}".length > 0){
            selectDeptList();
        }
    }
    function selectCityList() {
        var city = new City();
        city.provinceId = dwr.util.getValue("provinceId");
        AreaService.selectCityList(city, callbackSelectCityList);
    }
    function callbackSelectCityList(list) {
        dwr.util.removeAllOptions("cityId");
        var option = new Option("选择市", "");
        $("cityId").add(option);
        dwr.util.addOptions("cityId", list, "cityId", "cityName");
    }
    function selectDistrictList() {
        var district = new District();
        district.cityId = dwr.util.getValue("cityId");
        AreaService.selectDistrictList(district, callbackSelectDistrictList);
    }
    function callbackSelectDistrictList(list) {
        dwr.util.removeAllOptions("districtId");
        var option = new Option("选择区", "");
        $("districtId").add(option);
        dwr.util.addOptions("districtId", list, "districtId", "districtName");
    }
    function selectDeptList(){
        var dept = new Dept();
        dept.deptId = "${deptId}";
        DeptService.selectDeptUserList(dept, callbackSelectDeptList);
    }
    function callbackSelectDeptList(list){
        var deptUser = list[0];
        setModelToForm(deptUser.dept);
        dwr.util.setValue("deptTryDay", deptUser.dept.deptTryDay);
        var cityOption = new Option(deptUser.dept.city.cityName, deptUser.dept.city.cityId);
        var districtOption = new Option(deptUser.dept.district.districtName, deptUser.dept.district.districtId);
        $("cityId").add(cityOption);
        cityOption.selected = true;
        $("cityId").onclick = function () {
            if($("cityId").options.length == 2) {
                selectCityList();
            }
        }
        $("districtId").add(districtOption);
        districtOption.selected = true;
        $("districtId").onclick = function () {
            if($("districtId").options.length == 2) {
                selectDistrictList();
            }
        }
    }
    function submitDept() {
        if("${deptId}".length > 0){
            updateDept();
        } else {
            insertDept();
        }
    }
    function insertDept(){
        if(validate($("deptForm"))){
            var dept = dwr.util.getFormValues("deptForm");
            dept.deptCreateTime = new Date(dwr.util.getValue("deptCreateTime"));
            var length = $("deptLogoFile").files.length;
            if(length > 0) {
                var oFReader = new FileReader();
                oFReader.readAsDataURL($("deptLogoFile").files[0]);
                oFReader.onload = function (oFREvent) {
                    var base64 = oFREvent.target.result.split(",")[1];
                    dept.deptLogo = base64;
                    DeptService.insertDept(dept, callback);
                }
            } else {
                DeptService.insertDept(dept, callback);
            }
        }
    }
    function updateDept(){
        if(validate($("deptForm"))){
            var dept = dwr.util.getFormValues("deptForm");
            dept.deptCreateTime = new Date(dwr.util.getValue("deptCreateTime"));
            var length = $("deptLogoFile").files.length;
            if(length > 0) {
                var oFReader = new FileReader();
                oFReader.readAsDataURL($("deptLogoFile").files[0]);
                oFReader.onload = function (oFREvent) {
                    var base64 = oFREvent.target.result.split(",")[1];
                    dept.deptLogo = base64;
                    DeptService.updateDept(dept, callback);
                }
            } else {
                DeptService.updateDept(dept, callback);
            }
        }
    }
    function callback() {
        location = "${ctx}/dept/dept_list.jsp?deptParentId=${deptParentId}";
    }
</script>